package com.sskj.boom.ui.fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.boom.R2;
import com.sskj.boom.bean.OpenAwardBean;
import com.sskj.boom.presenter.OpenAwardHistoryPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;

import com.sskj.boom.R;
import com.sskj.lib.mvchelper.ModelRx2DataSource;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/06/27
 */

@Route(path = RConfig.BOOM_FRAGMENT_OPEN_AWARD_HISTORY)//开奖历史
public class OpenAwardHistoryFragment extends BaseFragment<OpenAwardHistoryPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    private MVCCoolHelper<List<OpenAwardBean>> mvcHelper;

    @Override
    public int getLayoutId() {
        return R.layout.boom_fragment_open_award_history;
    }

    @Override
    public OpenAwardHistoryPresenter getPresenter() {
        return new OpenAwardHistoryPresenter();
    }



    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        refresh();
    }


    @Override
    public void initView() {
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        slimAdapter = SlimAdapter.create().register(R.layout.boom_item_open_award, new SlimInjector<OpenAwardBean>() {
            @Override
            public void onInject(OpenAwardBean data, IViewInjector injector, List payloads) {
                if (slimAdapter.getData().indexOf(data) % 2 == 0) {
                    injector.background(R.id.llBg, ContextCompat.getDrawable(App.INSTANCE, R.drawable.lib_solidf7f_cor10));
                } else {
                    injector.backgroundColor(R.id.llBg, ContextCompat.getColor(App.INSTANCE, R.color.transparent));
                }
                injector.text(R.id.tv_periodID, data.getPeriodID())
                        .text(R.id.tv_boomValue, data.getBoomValue())
                        .text(R.id.tv_boomHash, data.getBoomHash())
                        .clicked(R.id.tv_check, view -> {
                            ARouter.getInstance().build(RConfig.BOOM_CHECKOUT).withString(Constans.BOOMHASH, data.getBoomHash()).navigation();
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.setAdapter(slimAdapter);
        ModelRx2DataSource<OpenAwardBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<OpenAwardBean>)
                        page -> mPresenter.getOpenAwardList(page, 10), 10);
        mvcHelper.setDataSource(dataSource);
        refresh();
    }

    @Override
    public void initData() {

    }


    private void refresh() {
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }


    public static OpenAwardHistoryFragment newInstance() {
        OpenAwardHistoryFragment fragment = new OpenAwardHistoryFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


}
